package com.showtao.common.security.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.codehaus.jackson.annotate.JsonIgnore;

import com.showtao.front.domain.BaseDomain;

@SuppressWarnings("serial")
@Entity
@Table(name = "st_role")
public class Role extends BaseDomain {

    @Id
    @Column(name = "role_name")
    private String roleName;
    @Column
    private String description;
    @JsonIgnore
    @ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
    @JoinTable(name = "st_role_permission", joinColumns = { @JoinColumn(name = "role_name", referencedColumnName = "role_name") }, inverseJoinColumns = { @JoinColumn(name = "permission", referencedColumnName = "permission") })
    @OrderBy("createdAt")
    private List<Permission> permissions;

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (!this.getClass().isInstance(obj)) {
            return false;
        }
        return new EqualsBuilder().append(roleName, ((Role) obj).getRoleName()).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(roleName).toHashCode();
    }
}
